/**
 * Lockbox
 * a Password and Secret Information Manager
 * View to choose the item type upon creation
 *
 * Created by Nathan Black
 * 
 * Licensed under GPL v2
 */
 
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <UIKit/UIApplication.h>
#import <UIKit/UITable.h>
#import "LockboxItemTypeView.h"
#import "LockboxItem.h"
#import "LockboxApp.h"

@interface LockboxItemTypeView (Private)


@end


@implementation LockboxItemTypeView

- (void)dealloc
{
    [_navBar release];
    [_prefsTable release];
    [_typeGroupCell release];
    [_passwordTypeCell release];
    [_cardTypeCell release];
    [_otherTypeCell release];

    [super dealloc];
}

///////////// Constructor ////////////////
- (id)initWithFrame:(struct CGRect)frame app:(id)app
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _app = app;
        
        _navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, frame.size.width, LBITV_NAVBAR_HEIGHT)];
        [_navBar showButtonsWithLeftTitle: @"Cancel" rightTitle:NULL leftBack: TRUE];
        [_navBar setBarStyle: 3];
        [_navBar enableAnimation];
        [_navBar setDelegate: self];

        UINavigationItem* navBarLabel = [[[UINavigationItem alloc] initWithTitle:@"Choose Type"] autorelease];
        [_navBar pushNavigationItem:navBarLabel];

        _typeGroupCell = [[UIPreferencesTableCell alloc] init];
        [_typeGroupCell setTitle: @"Item Type"];
        
        _passwordTypeCell = [[UIPreferencesTableCell alloc] init];
        [_passwordTypeCell setValue:@"Password"];
        [_passwordTypeCell setShowDisclosure:YES];
        _cardTypeCell = [[UIPreferencesTableCell alloc] init];
        [_cardTypeCell setValue:@"Card"];
        [_cardTypeCell setShowDisclosure:YES];
        _otherTypeCell = [[UIPreferencesTableCell alloc] init];
        [_otherTypeCell setValue:@"Other"];
        [_otherTypeCell setShowDisclosure:YES];

        frame.origin.y = LBITV_NAVBAR_HEIGHT;
        frame.size.height -= LBITV_NAVBAR_HEIGHT;
        _prefsTable = [[UIPreferencesTableEx alloc] initWithFrame: frame];
        [_prefsTable setDataSource: self];
        [_prefsTable setDelegate: self];

        [self addSubview: _navBar];
        [self addSubview: _prefsTable];
        [_prefsTable reloadData];
    }
    
    return self;
}


- (void)navigationBar:(UINavigationBar*)navbar buttonClicked:(int)button
{
    switch (button) 
    {
        case 1: // Back
            [_app transitionToView:[(LockboxApp*)_app getListView] withTransition:2];
            break;

        case 0: // right button
            break;
    }
}

- (void)newItem
{
    [_prefsTable selectRow:-1 byExtendingSelection:FALSE];
}


- (void)tableRowSelected:(NSNotification *)notification
{
    int itemType = [_prefsTable selectedRow] - 1;
    if (itemType >= 0 && itemType < 3) {
        LockboxItem* item = [[[LockboxItem alloc] init] autorelease];
        item->type = (enum LockboxItemType)itemType;
        
        [_app transToItemView:item cell:NULL];
    }
}
/*- (BOOL) table:(UITable *) table canInsertAtRow:(int) row 
{
    return NO;
}*/


// Start of Preference required methods
- (int) numberOfGroupsInPreferencesTable: (UIPreferencesTable*)table 
{
    NSLog(@"ItemTypeView numberOfGroupsInPreferencesTable");
    return 1;
}

- (int) preferencesTable: (UIPreferencesTable*)table numberOfRowsInGroup: (int)group 
{
    NSLog(@"ItemTypeView numberOfRowsInGroup: %d", group);
    return (group == 0 ? 3 : 0);
}

- (UIPreferencesTableCell*) preferencesTable: (UIPreferencesTable*)table cellForGroup: (int)group 
{
    NSLog(@"ItemTypeView cellForGroup: %d", group);
    if (group == 0)
        return _typeGroupCell;
    else
        return NULL;
}

- (UIPreferencesTableCell*) preferencesTable: (UIPreferencesTable*)table cellForRow: (int)row inGroup: (int)group 
{
    NSLog(@"ItemTypeView cellForRow: %d inGroup: %d", row, group);
    if (group == 0) {
        switch (row) {
            case 0:
                return _passwordTypeCell;
            case 1:
                return _cardTypeCell;
            case 2:
                return _otherTypeCell;
        }
    }
    else {
        // TODO NSAssert(@"numberOfRowsInGroup asked for an unknown group", false);
        return NULL;
    }
}

- (float) preferencesTable: (UIPreferencesTable*)table heightForRow: (int)row inGroup: (int)group withProposedHeight: (float)proposed 
{
    //NSLog(@"ItemTypeView heightForRow: %d group: %d withProposedHeight: %4.1f", row, group, proposed);
    if (row == -1) return proposed * 3.5;

    return proposed;
}
// End of Preferences required modules

/*- (NSMethodSignature*)methodSignatureForSelector:(SEL)selector {
    NSLog(@"LBItemTypeView: Requested method for selector: %@", NSStringFromSelector(selector));
    return [super methodSignatureForSelector:selector];
}

- (BOOL)respondsToSelector:(SEL)aSelector {
    NSLog(@"LBItemTypeView: Request for selector: %@", NSStringFromSelector(aSelector));
    return [super respondsToSelector:aSelector];
}

- (void)forwardInvocation:(NSInvocation *)anInvocation {
    NSLog(@"LBItemTypeView: Called from: %@", NSStringFromSelector([anInvocation selector]));
    [super forwardInvocation:anInvocation];
}*/

@end
